﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Sockets;
using System.IO;
using SWSoft.Framework;
using System.Data;
using SIIMWeb.YIBAO;
using System.Text;
using SWSoft.Framework.Web;
using System.Reflection;
using SWSoft.Caller.Framework;

namespace TradeTestWeb
{
    public partial class Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlylbxid.DataSource = DBVisitor.ExecuteTable("SELECT * FROM yb_bxlbxx where bxqypb=1 ORDER BY ylbxid asc");
                ddlylbxid.DataTextField = "YLBXMC";
                ddlylbxid.DataValueField = "HZSRM3";
                ddlylbxid.DataBind();
                ddlylbxid_SelectedIndexChanged(sender, e);
            }
        }

        protected void ddlylbxid_SelectedIndexChanged(object sender, EventArgs e)
        {
            var table = new DataTable();
            table.Columns.Add("ylbxid");
            table.Columns.Add("Name");
            var director = new DirectoryInfo(new DirectoryInfo(Server.MapPath("~")).Parent.FullName + "\\Tools\\TradeTest\\" + ddlylbxid.SelectedValue);
            if (!director.Exists)
            {
                Directory.CreateDirectory(director.FullName);
            }
            foreach (var item in Directory.GetFiles(director.FullName, "*_IN.XML"))
            {
                table.Rows.Add(ddlylbxid.SelectedValue, Path.GetFileNameWithoutExtension(item).ToUpper());
            }
            repparams.DataSource = table;
            repparams.DataBind();
        }

        public string tradecall(string ybname, string jyname, string url)
        {
            Response.ContentType = "text/xml;charset=utf-8";
            var xcontent = File.ReadAllText(new DirectoryInfo(Server.MapPath("~")).Parent.FullName + "\\Tools\\TradeTest\\" + ybname + "\\" + jyname + ".xml", Encoding.UTF8);
            var str = YIBAOJK.ZMQCall(ybname, jyname.Replace("_IN", ""), "tcp://" + url + ":20000", xcontent);
            var path = new DirectoryInfo(Server.MapPath("~")).Parent.FullName + "\\Tools\\logs\\" + ybname;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            File.WriteAllText(string.Format("{0}\\{1}{2:yyyyMMddHHmmss}.xml", path, jyname, DateTime.Now), str);
            return str;
        }

        public string tradejson(string jyname, string canshu)
        {
            var ass = Assembly.GetAssembly(typeof(RENYUANXX_IN));
            var type = ass.GetType(jyname + "_IN");
            dynamic typeins = Activator.CreateInstance(type, null);
            var basetype = ass.GetType(jyname + "_INBASEINFO");
            var infotype = ass.GetType(jyname + "_IN" + jyname);
            dynamic baseinfo = Activator.CreateInstance(basetype);
            typeins.Items = new Object[2];
            baseinfo.CAOZUOYDM = "DBA";
            baseinfo.CAOZUOYXM = "管理员";
            baseinfo.CAOZUORQ = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            baseinfo.XITONGBS = "WebTest";
            baseinfo.FENYUANDM = "1";
            typeins.Items[0] = baseinfo;

            var info = Activator.CreateInstance(infotype);
            var kv = new KeyParameter(canshu);
            foreach (var item in kv.Items)
            {
                SetValue(info, item.Key, item.Value);
            }
            typeins.Items[1] = info;
            return "{\"result\":\"" + Server.HtmlEncode(YIBAOJK.ZMQCall(kv["yb"], jyname, "tcp://" + kv["url"] + ":20000", ObjectUtil.GetXml(typeins))) + "\"}";
        }

        public void SetValue(object obj, string name, string value)
        {
            var property = obj.GetType().GetProperty(name);
            if (property != null)
            {
                property.SetValue(obj, value, null);
            }
        }
    }
}